package com.liuhaoqi.raletiveblog.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Blog;
import com.liuhaoqi.raletiveblog.entity.dto.BlogDto;
import com.liuhaoqi.raletiveblog.mapper.BlogMapper;
import com.liuhaoqi.raletiveblog.result.business.PageResult.AddBlogPageResult;
import com.liuhaoqi.raletiveblog.service.BlogService;
import com.liuhaoqi.raletiveblog.utils.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/17 11:22
 * @Version 1.0
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogMapper blogMapper;

    @Override
    public int getBlogsnum() {
        return blogMapper.getBlogsnum();
    }

    @Override
    public int updateBlog(Blog blog) {
        return blogMapper.updateBlog(blog);
    }

    @Override
    public int deleteBlogByBlogId(Long id) {
        return blogMapper.deleteBlogByBlogId(id);
    }

    @Override
    public List<AddBlogPageResult> getAdminBlogDtoList(int start,int end,int pagesize) {
        List<AddBlogPageResult> adminBlogDtoList = blogMapper.getAdminBlogDtoList(start,end,pagesize);
        return adminBlogDtoList;
    }

    @Override
    public Long getBlogByCreateTime() {
        return blogMapper.getBlogByCreateTime();
    }

    @Override
    public int updateBlogCommentNumByBLogId(Long id) {
        return blogMapper.updateBlogCommentNumByBLogId(id);
    }

    @Override
    public int getBlogViewsById(Long blogId) {
        return blogMapper.getBlogViewsById(blogId);
    }

    @Override
    public int insertBlog(Blog blog) {
        return blogMapper.insert(blog);
    }

    @Override
    public List<BlogDto> getBlogListNoFenYe() {
        return blogMapper.getBlogListNoFenYe();
    }

    @Override
    public PageInfo<BlogDto> getBlogListByCategoryname(String categoryName,String pageNum,String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<BlogDto> blogListByCategoryname = blogMapper.getBlogListByCategoryname(categoryName);
        PageInfo<BlogDto> blogDtoPageInfo = new PageInfo<>(blogListByCategoryname);
        return blogDtoPageInfo;
    }

    @Override
    public int updateDetailBlogViewsById(Long id) {
        return blogMapper.updateDetailBlogViewsById(id);
    }

    @Override
    public PageInfo<BlogDto> getBlogList(String pageNum, String pageSize) {
        int num=Integer.parseInt(pageNum);
        int size=Integer.parseInt(pageSize);
        PageHelper.startPage(num,size);
        List<BlogDto> blogList = blogMapper.getBlogList();
        PageInfo<BlogDto> pageInfo = new PageInfo<>(blogList);
        //查询得到user和category时
        HashMap<String, Object> map = new HashMap<>();
        /*
        ////当前页的内容
        //是否是最后一页
        //总共有多少页
        //总共有多少条数据
        //总共有多少条数据
        //当前页是第几页
        //当前页是否是第一页
         */
//        map.put("content",pageInfo.getList());
//        map.put("last",pageInfo.isIsLastPage());
//        map.put("totalPages",pageInfo.getPages());
//        map.put("totalElements",pageInfo.getTotal());
//        map.put("size",pageInfo.getSize());
//        map.put("number",pageInfo.getPageNum());
//        map.put("first",pageInfo.isIsFirstPage());
        return pageInfo;
    }

    @Override
    public BlogDto
    getDetailBlog(Long id) {
        BlogDto BlogDto = blogMapper.getDetailBlogById(id);

        return BlogDto;
    }

    @Override
    public Blog getDetailBlogAndConvert(Long id) {
        Blog blog = blogMapper.getDetailBlogAndConvert(id);
        Blog b =new Blog();
        BeanUtils.copyProperties(blog,b);
        String content=b.getContent();
        String s = MarkdownUtils.markdownToHtmlExtensions(content);
        b.setContent(s);
        return b;
    }

    @Override
    public List<BlogDto> getBlogDtoTag() {
        return blogMapper.getBlogDtoTag();
    }
}
